import DataSet, { DataSetProps } from 'choerodon-ui/pro/lib/data-set/DataSet';
import { FieldType } from 'choerodon-ui/pro/lib/data-set/enum';
import request from 'utils/request';
import { API_HOST } from 'utils/config';
import { getUrlParamHref, getLovDefineAxiosConfig, getLovQueryAxiosConfig } from './utils';

export const TableListDataSetConfig = (lineBoCode): DataSetProps => {
  const lineTypeDS = new DataSet({
    data: [
      { value: 'service', meaning: '人工' },
      { value: 'item', meaning: '物料' },
    ]
  })
  return {
    autoQuery: false,
    autoCreate: false,
    // selection: false,
    pageSize: 10,
    paging: false,
    transport: {
      submit: ({ data }) => {
        return {
          url: `/hmde/v1/executor/${lineBoCode}/batch-update`,
          method: 'POST',
          data,
        };
      },
      read: ({ params }) => {
        return {
          url: `/hmde/v1/executor/${lineBoCode}/page`,
          method: 'GET',
          params,
        };
      },
      destroy: ({ data }) => {
        return {
          url: `/hmde/v1/executor/${lineBoCode}/batch-delete`,
          method: 'DELETE',
          data,
        };
      },
    },
    fields: [
      {
        name: 'lineNum',
        type: FieldType.number,
        label: '行号',
      },
      {
        name: 'lineType',
        type: FieldType.string,
        label: '分类',
        options: lineTypeDS,
      },
      {
        name: 'itemId_lovObj',
        type: FieldType.object,
        label: '物料',
        lovCode: 'MDM_ITEM',
        textField: 'itemName',
        lovPara: {
          '@publishFlag': getUrlParamHref('publishFlag') === 'false' ? false : undefined,
        },
        lovDefineAxiosConfig: getLovDefineAxiosConfig({
          lovType: 'BUSINESS_OBJECT_OPTION',
          businessObjectOptionCode: 'MDM_ITEM',
        }) as any,
        lovQueryAxiosConfig: getLovQueryAxiosConfig({ viewCode: 'MDM_ITEM' }),
      },
      {
        name: 'itemId',
        bind: 'itemId_lovObj.itemId',
      },
      {
        name: 'lineQuantity',
        type: FieldType.number,
        precision: 2,
        label: '数量',
      },
      {
        name: 'unitPrice',
        type: FieldType.number,
        precision: 2,
        label: '单价',
      },
      {
        name: 'lineAmount',
        type: FieldType.number,
        label: '行金额',
      }
    ],
    queryFields: [
      { name: 'lineNum', type: FieldType.string, label: '行号' },
      { name: 'lineType', type: FieldType.string, label: '分类', options: lineTypeDS },
    ],
    events: {
      update: ({ name, record }) => {
        if(['lineQuantity', 'unitPrice'].includes(name)) {
          const lineQuantity = record.get('lineQuantity');
          const unitPrice = record.get('unitPrice');
          if(typeof lineQuantity === 'number' && typeof unitPrice === 'number')
          record.set('lineAmount', lineQuantity * unitPrice);
        }
      }
    }
  };
};

export const saveAll = ({ header, body, headerBoCode }) => {
  return request(
    `${API_HOST}/hmde/v1/executor/${headerBoCode}/batch-update`,
    {
      method: 'POST',
      data: body,
      headers: {
        ...header,
      },
    },
  );
};
